package cn.gemiman.daoyi.module.cms.service.tagdictionary;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gemiman.daoyi.module.cms.controller.admin.tagdictionary.vo.TagDictionaryPageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.tagdictionary.vo.TagDictionarySaveReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogmessage.BlogMessageDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.tagdictionary.TagDictionaryDO;
import cn.gemiman.daoyi.module.cms.dal.mysql.tagdictionary.TagDictionaryMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.cms.enums.ErrorCodeConstants.TAG_DICTIONARY_NOT_EXISTS;

/**
 * 文章标签 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class TagDictionaryServiceImpl implements TagDictionaryService {

    @Resource
    private TagDictionaryMapper tagDictionaryMapper;

    @Override
    public Long createTagDictionary(TagDictionarySaveReqVO createReqVO) {
        // 插入
        TagDictionaryDO tagDictionary = BeanUtils.toBean(createReqVO, TagDictionaryDO.class);
        tagDictionaryMapper.insert(tagDictionary);
        // 返回
        return tagDictionary.getId();
    }

    @Override
    public void updateTagDictionary(TagDictionarySaveReqVO updateReqVO) {
        // 校验存在
        validateTagDictionaryExists(updateReqVO.getId());
        // 更新
        TagDictionaryDO updateObj = BeanUtils.toBean(updateReqVO, TagDictionaryDO.class);
        tagDictionaryMapper.updateById(updateObj);
    }

    @Override
    public void deleteTagDictionary(Long id) {
        // 校验存在
        validateTagDictionaryExists(id);
        // 删除
        tagDictionaryMapper.deleteById(id);
    }

    private void validateTagDictionaryExists(Long id) {
        if (tagDictionaryMapper.selectById(id) == null) {
            throw exception(TAG_DICTIONARY_NOT_EXISTS);
        }
    }

    @Override
    public TagDictionaryDO getTagDictionary(Long id) {
        return tagDictionaryMapper.selectById(id);
    }

    @Override
    public List<String> getTagDictionary() {
        return tagDictionaryMapper.selectList(new LambdaQueryWrapperX<TagDictionaryDO>().orderByDesc(TagDictionaryDO::getArticleCount).orderByDesc(TagDictionaryDO::getId)).stream().map(TagDictionaryDO::getName).distinct().toList();
    }

    @Override
    public PageResult<TagDictionaryDO> getTagDictionaryPage(TagDictionaryPageReqVO pageReqVO) {
        return tagDictionaryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TagDictionaryDO> getHotMessageTagList(String appId) {
        return tagDictionaryMapper.selectHotMessageTagList(appId);
    }

}
